################################################################################
# Created By:Pietryka
# Creation Date:  2016-08-22
# Purpose: tables to display conditional logits from cces data
# Questions: mpietryka@fsu.edu
################################################################################


# PREAMBLE =============================================



# LOAD PACKAGES -----------------

library(tidyverse)
library(texreg)




# LOAD DATA -----------------

# Workspace from 'CCES3-Analysis-Fixed-Effects.R'
load("Data/CCES3-Analysis-Fixed-Effects.Rdata")


# FUNCTION TO EXTRACT MODEL RESULTS  -----------------------

extract_mod <-
  function (mod,
            include.aic = TRUE,
            include.bic = TRUE,
            include.nobs = TRUE,
            ...)
  {
    coefficient.names <- colnames(mod$q.mi)
    coefficients <- c(mod$q.mi)
    standard.errors <- c(mod$se.mi)
    n <- as.integer(mod$n)
    aic <- mod$aic
    bic <- mod$bic
    df <- n - length(coefficients) - 1
    t_stats = abs(coefficients/standard.errors)
    significance <- pt(t_stats, df = df, lower.tail = FALSE) * 2
    gof <- numeric()
    gof.names <- character()
    gof.decimal <- logical()
    if (include.aic == TRUE) {
      gof <- c(gof, aic)
      gof.names <- c(gof.names, "AIC")
      gof.decimal <- c(gof.decimal, TRUE)
    }
    if (include.bic == TRUE) {
      gof <- c(gof, bic)
      gof.names <- c(gof.names, "BIC")
      gof.decimal <- c(gof.decimal, TRUE)
    }
    if (include.nobs == TRUE) {
      gof <- c(gof, n)
      gof.names <- c(gof.names, "N")
      gof.decimal <- c(gof.decimal, FALSE)
    }
    tr <- createTexreg(coef.names = coefficient.names, coef = coefficients,
                       se = standard.errors, pvalues = significance,
                       gof.names = gof.names,
                       gof = gof, gof.decimal = gof.decimal)
    return(tr)
}

# Labels and Titles  =================================================

# VARIABLE LABELS ------------------------

var_map <- list(
  parent = "Parent of young child (0 = No; 1 = Yes)",
  `factor(year)2014` = "2014 elections (0 = 2010; 1 = 2014)",
  collgrad = "College graduate (0 = No; 1 = Yes)",
  contacted = "Contacted by political party (0 = No; 1 = Yes)",
  `factor(famincquart)2` = "In second income quartile (0 = No; 1 = Yes)",
  `factor(famincquart)3` = "In third incomequartile (0 = No; 1 = Yes)",
  `factor(famincquart)4` = "In fourth income quartile (0 = No; 1 = Yes)",
  married = "Is married (0 = No; 1 = Yes)",
  countymove_change = "County (0 = Remained the same; 1 = Changed)",
  homeowner = "Owns home (0 = No; 1 = Yes)",
  student = "Student (0 = No; 1 = Yes)",
  employfull = "Employed fulltime (0 = No; 1 = Yes)",
  churchfreq = "Church attendance frequency",
  newsint = "Political interest"
)


# MODEL TITLES ------------------------

model_labels <- c(
  "turnout" = "Turnout",
  "attendmeeting" = "Attend Meeting",
  "polsign" = "Display Sign",
  "workcampaign" = "Work for Campaign",
  "donatemoney" = "Donate Money"
)


# IDENTIFY RELEVANT MODELS  ------------------------

form1_mods <- which(formulae$equation == "form1")
form2_mods <- which(formulae$equation == "form2")
form3_mods <- which(formulae$equation == "form3")
mod_names <- model_labels[formulae$y[form1_mods]]


# FORMAT THE MODELS  ------------------------

extract1 <- map(combined_alldata[form1_mods], extract_mod)
extract2 <- map(combined_alldata[form2_mods], extract_mod)
extract3 <- map(combined_alldata[form3_mods], extract_mod)


# CREATE THE TABLES  =============================================

htmlreg(extract1,
          caption = "Table B1. Baseline models",
          caption.above = TRUE,
          stars = 0.05,
          custom.coef.map = var_map,
          custom.model.names = mod_names,
          single.row = TRUE,
          custom.note = "* p < 0.05. Results based on 20 imputations. Source: 2010-14 CCES Panel Study.",
          file = "Tables/table-CCES-baseline.html"
)

htmlreg(extract2,
          caption = "Table B2. Intermediate models",
          caption.above = TRUE,
          stars = 0.05,
          custom.coef.map = var_map,
          custom.model.names = mod_names,
          single.row = TRUE,
          custom.note = "* p < 0.05. Results based on 20 imputations. Source: 2010-14 CCES Panel Study.",
     file = "Tables/table-CCES-intermediate.html"
)

htmlreg(extract3,
          caption = "Table B3. Full models",
          caption.above = TRUE,
          stars = 0.05,
          custom.coef.map = var_map,
          custom.model.names = mod_names,
          single.row = TRUE,
          custom.note = "* p < 0.05. Results based on 20 imputations. Source: 2010-14 CCES Panel Study.",
     file = "Tables/table-CCES-full.html"
)

